###########################################
##REPLICATION PACKAGE FOR: ################
#The Long Run Consequences of The Opium ###
#Concessions on Ethnic Animosity in Java###
#######NKUIPERS@BERKELEY.EDU###############
###########################################

rm(list=ls())

library(foreign)
library(readstata13)
library(geosphere)
library(multiwayvcov)
library(lmtest)
library(measurements)
library(tidyverse)
library(estimatr)
library(maptools)
library(rgdal)
library(rgeos)
library(gridExtra)
options(geonamesUsername="nkuipers")
library(geonames)
library(raster)
library(ineq)
library(magrittr)
library(stringr)
library(stargazer)

setwd("~/Dropbox/Opium/replication_file_wp")

##-----------------------------------------------------------------------------------------------------------------------------------------

########################
##I. DATA PREP &########
## LOAD FUNCTIONS#######
########################

source("./_2_code/functions.R")

#the following two scripts are data-cleaning scripts:
#(1) source("./code/data_cleaning/make_data.R")
#(2) source("./code/data_cleaning/make_village_distances.R")

#replicators cannot run these scripts because you will not have access to the underlying sensitive data used to create the resulting
#analysis datasets. The only sensitive data are the coordinates of the sampled IFLS villages. You may request these data from the
#RAND corporation. I have include the scripts if you would like to look them through, or plug in the coordinate data if you are given acces.
#Please don't hesitate to contact me if you have questions about this process.

#the relevant de-personalized outputs are saved in the replication file, which I load in below. 

#(1) "./data/analysis_data/analysis_df_individual_level.csv"
#(2) "./data/analysis_data/analysis_df_cluster_level.csv"
#(3) "./data/maps/distances_csvs/village_distance.csv"


individual_level <- read.csv("./_3_data/_cleaned_data/analysis_df_individual_level.csv")
cluster_level = read.csv("./_3_data/_cleaned_data/analysis_df_cluster_level.csv")

keep_objs = lsf.str() %>% as.vector() #naming the objects we want to keep in the environment throughout
keep_objs = append(keep_objs, c("individual_level", "cluster_level", "keep_objs"))


########################
##II. FIGURES###########
########################
source("./_2_code/_b_figures/figure_1.R") #revenue of different tax farm plots
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/_b_figures/figure_2.R") #makes the maps, along with the IFLS village locations
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/_b_figures/figure_3.R") #makes the main outcome plots
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/_b_figures/figure_4.R") #makes the main outcome plot, with bw specified
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/_b_figures/figure_5.R") #makes the salt concession plot, with bw specified
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/_b_figures/figure_6.R") #makes the placebo boundary plot
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/_b_figures/figure_7.R") #makes the ethnicity HTE plot
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/_b_figures/figure_8.R") #makes the inequality HTE plot
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/_b_figures/figure_appendix_2abcd.R") #makes first four balance plots
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/_b_figures/figure_appendix_2e.R") #make fifth balance plot
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/_b_figures/figure_appendix_3.R") #make cluster collapsed analysis
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/_b_figures/figure_appendix_4.R") #make analysis with admin fixed effects
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/_b_figures/figure_appendix_5.R") #make analysis with terrain controls 
rm(list=setdiff(ls(), keep_objs)) #clean up

########################
##III. TABLES###########
########################

source("./_2_code/_c_tables/table_1.R") #look at inequality in 1930 census
rm(list=setdiff(ls(), keep_objs)) #clean up

source("./_2_code/_c_tables/table_appendix_2.R") #border controls
rm(list=setdiff(ls(), keep_objs)) #clean up

